package com.freak.neteasecloudmusic.modules.mine.fragment;

import android.os.Bundle;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.entity.MultiItemEntity;
import com.freak.neteasecloudmusic.R;
import com.freak.neteasecloudmusic.base.BaseAbstractMvpFragment;
import com.freak.neteasecloudmusic.modules.find.recommend.songlist.detail.DetailActivity;
import com.freak.neteasecloudmusic.modules.mine.entity.AddEntity;
import com.freak.neteasecloudmusic.modules.mine.entity.MinePlayListEntity;
import com.freak.neteasecloudmusic.modules.mine.fragment.adapter.MySongListAdapter;
import com.freak.neteasecloudmusic.net.log.LogUtil;
import com.freak.neteasecloudmusic.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MySongListFragment extends BaseAbstractMvpFragment<MySongListPresenter> implements MySongListContract.View {
    @BindView(R.id.recyclerViewMySongList)
    RecyclerView recyclerViewMySongList;
    private MySongListAdapter mySongListAdapter;
    private List<MultiItemEntity> list;
    private Bundle bundle;
    private int type;

    @Override
    protected MySongListPresenter createPresenter() {
        return new MySongListPresenter();
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_song_list;
    }

    @Override
    protected void initEventAndData() {
//        mPresenter.getPlaylist((String) SPUtils.get(App.getInstance().getApplicationContext(), Constants.UID, ""));

    }

    @Override
    protected void initView(View view) {
        bundle = getArguments();
        if (bundle == null) {
            return;
        }
        type = bundle.getInt("type");
        list = new ArrayList<>();
        mySongListAdapter = new MySongListAdapter(list, mActivity);
        recyclerViewMySongList.setLayoutManager(new GridLayoutManager(mActivity, 2));
        recyclerViewMySongList.setAdapter(mySongListAdapter);
        mySongListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                switch (adapter.getItemViewType(position)) {
                    case MySongListAdapter.ITEM_ADD:
                        break;
                    case MySongListAdapter.ITEM_CONTEXT:
                        MinePlayListEntity.PlaylistBean playlistsEntity= (MinePlayListEntity.PlaylistBean) adapter.getData().get(position);
                        Bundle bundle=new Bundle();
                        bundle.putString("id",playlistsEntity.getId());
                        gotoActivity(DetailActivity.class,false,bundle);
                        break;
                }
            }
        });
    }

    @Override
    protected void initLazyData() {

    }

    @Override
    protected void showLoading() {

    }

    @Override
    public void showToast(String toast) {
        ToastUtil.shortShow(toast);
    }

    /**
     * 播放列表
     *
     * @param model
     */
    @Override
    public void getPlaylistSuccess(MinePlayListEntity model) {
        list.clear();
        if (model != null) {
            if (model.getPlaylist() != null && model.getPlaylist().size() > 0) {
                switch (type) {
                    case 0:
                        for (int i=1;i<model.getPlaylist().size();i++){
                            if (!model.getPlaylist().get(i).isSubscribed()) {
                                list.add(model.getPlaylist().get(i));
                            }
                        }
                        AddEntity addEntity = new AddEntity();
                        list.add(addEntity);
                        break;
                    case 1:
                        for (MinePlayListEntity.PlaylistBean playlistBean : model.getPlaylist()) {
                            if (playlistBean.isSubscribed()) {
                                list.add(playlistBean);
                            }
                        }
                        break;
                }
            }
        }
        LogUtil.d(list);
        mySongListAdapter.notifyDataSetChanged();
    }


}
